<!DOCTYPE html>
<html lang="zh_cn">
<head>
          <title>来玩魔王的咚</title>
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <meta charset="utf-8" />
        <!-- twitter card metadata -->
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="/images/mowang.png">
<meta name="twitter:site" content="">
<meta name="twitter:title" content="Linux 初级知识点">
<meta name="twitter:description" content="<p>Linux文件系统(FHS)；结束进程(kill)；vi编辑器操作；shell输出带颜色的字体：修改命令提示符颜色</p>">
        <!-- OG Tags -->
<meta property="og:url" content="/linux-basic-knowledge-points.html"/>
<meta property="og:title" content="来玩魔王的咚 | Linux 初级知识点" />
<meta property="og:description" content="<p>Linux文件系统(FHS)；结束进程(kill)；vi编辑器操作；shell输出带颜色的字体：修改命令提示符颜色</p>" />
        <!-- favicon -->
        <link rel="icon" type="image/png" href="/images/mowang.png">
        <!-- moment.js for date formatting -->
        <script src="/theme/js/moment.js"></script>
        <!-- css -->
        <link rel="stylesheet" type="text/css" href="/theme/css/main.css" />
        <!-- 左边的menu，如果页面高度不够，就跟着滚动，否则文章分类显示不全 -->
        <link rel="stylesheet" type="text/css" href="/theme/css/mycss/menu.css" />
		<script>
			
                /*! grunt-grunticon Stylesheet Loader - v2.1.2 | https://github.com/filamentgroup/grunticon | (c) 2015 Scott Jehl, Filament Group, Inc. | MIT license. */
    
    (function(e){function t(t,n,r,o){"use strict";function a(){for(var e,n=0;u.length>n;n++)u[n].href&&u[n].href.indexOf(t)>-1&&(e=!0);e?i.media=r||"all":setTimeout(a)}var i=e.document.createElement("link"),l=n||e.document.getElementsByTagName("script")[0],u=e.document.styleSheets;return i.rel="stylesheet",i.href=t,i.media="only x",i.onload=o||null,l.parentNode.insertBefore(i,l),a(),i}var n=function(r,o){"use strict";if(r&&3===r.length){var a=e.navigator,i=e.Image,l=!(!document.createElementNS||!document.createElementNS("http://www.w3.org/2000/svg","svg").createSVGRect||!document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#Image","1.1")||e.opera&&-1===a.userAgent.indexOf("Chrome")||-1!==a.userAgent.indexOf("Series40")),u=new i;u.onerror=function(){n.method="png",n.href=r[2],t(r[2])},u.onload=function(){var e=1===u.width&&1===u.height,a=r[e&&l?0:e?1:2];n.method=e&&l?"svg":e?"datapng":"png",n.href=a,t(a,null,null,o)},u.src="",document.documentElement.className+=" grunticon"}};n.loadCSS=t,e.grunticon=n})(this);(function(e,t){"use strict";var n=t.document,r="grunticon:",o=function(e){if(n.attachEvent?"complete"===n.readyState:"loading"!==n.readyState)e();else{var t=!1;n.addEventListener("readystatechange",function(){t||(t=!0,e())},!1)}},a=function(e){return t.document.querySelector('link[href$="'+e+'"]')},c=function(e){var t,n,o,a,c,i,u={};if(t=e.sheet,!t)return u;n=t.cssRules?t.cssRules:t.rules;for(var l=0;n.length>l;l++)o=n[l].cssText,a=r+n[l].selectorText,c=o.split(");")[0].match(/US\-ASCII\,([^"']+)/),c&&c[1]&&(i=decodeURIComponent(c[1]),u[a]=i);return u},i=function(e){var t,o,a;o="data-grunticon-embed";for(var c in e)if(a=c.slice(r.length),t=n.querySelectorAll(a+"["+o+"]"),t.length)for(var i=0;t.length>i;i++)t[i].innerHTML=e[c],t[i].style.backgroundImage="none",t[i].removeAttribute(o);return t},u=function(t){"svg"===e.method&&o(function(){i(c(a(e.href))),"function"==typeof t&&t()})};e.embedIcons=i,e.getCSS=a,e.getIcons=c,e.ready=o,e.svgLoadedCallback=u,e.embedSVG=u})(grunticon,this);
                
                grunticon(["/theme/css/icons.data.svg.css", "/theme/css/icons.data.png.css", "/theme/css/icons.fallback.css"]);
            </script>
        <noscript><link href="/theme/css/icons.fallback.css" rel="stylesheet"></noscript>
        <!-- menu toggle javascript -->
        <script type="text/javascript">
            document.addEventListener("DOMContentLoaded", initMenu);
            
            function initMenu(){
                var menu = document.getElementById("menu");
                var menulink = document.getElementById("menu-link");
                menulink.addEventListener("click", function toggleMenu(){
                        window.event.preventDefault();
                        menulink.classList.toggle('active');
                        menu.classList.toggle('active');              
                    });
            };
        </script>
        <!-- 不蒜子 -->
        <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

    <meta name="description" content="<p>Linux文件系统(FHS)；结束进程(kill)；vi编辑器操作；shell输出带颜色的字体：修改命令提示符颜色</p>" />

    <meta name="tags" content="kill" />
    <meta name="tags" content="SecureCRT" />
    <meta name="tags" content="vi" />
  <!-- 替换部分base的样式，看文章时，要再宽一点，右边有很多空间可以撑开 -->
  <link rel="stylesheet" type="text/css" href="/theme/css/mycss/article.css" />

</head>
<body>
    <div role="banner" id="masthead">
        <header>
            <a href="/"><img src="/images/mowang.png" alt="McManus Logo"></a>
                <h1>来玩魔王的咚@骑士救兵</h1>
            <a href="#menu" id="menu-link">more stuff</a>
            <nav id="menu">
                <ul>
                        <li><a href="/tags">tags</a></li>
                            <li><a href="/category/cloud.html">Cloud</a></li>
                            <li><a href="/category/docker.html">Docker</a></li>
                            <li><a href="/category/go.html">Go</a></li>
                            <li class="active"><a href="/category/linux.html">Linux</a></li>
                            <li><a href="/category/python.html">Python</a></li>
                            <li><a href="/category/xue-xi-bi-ji.html">学习笔记</a></li>
                            <li><a href="/category/yun-wei-zi-dong-hua.html">运维自动化</a></li>
                </ul>
            </nav>
        </header>
    </div>
        <div class="page" role="main">
  <div class="article" role="article">
    <article>
        <footer>
            <a name="top"></a>
            <p>
              <time datetime=" 2020-08-15 13:00:00+08:00">
                <script>document.write(moment('2020-08-15 13:00:00+08:00').format('LL'));</script>
              </time>
              ~
              <time datetime=" 2020-08-15 13:00:00+08:00">
                <script>document.write(moment('2020-11-06 14:00:00+08:00').format('LL'));</script>
              </time>
            </p>
        </footer>
        <header>
          <h2>
            Linux 初级知识点
          </h2>
        </header>
      <div class="content">
         <div class="toc">
<ul>
<li><a href="#linux-wen-jian-xi-tong">Linux 文件系统</a></li>
<li><a href="#jie-shu-jin-cheng">结束进程</a><ul>
<li><a href="#linux-jin-cheng-xin-hao">Linux进程信号</a></li>
<li><a href="#kill-ming-ling">kill命令</a></li>
<li><a href="#killall-ming-ling">killall命令</a></li>
</ul>
</li>
<li><a href="#vi-bian-ji-qi">vi编辑器</a><ul>
<li><a href="#xiu-gai-wen-ben">修改文本</a></li>
<li><a href="#ding-wei-ming-ling">定位命令</a></li>
<li><a href="#che-xiao-he-hui-fu">撤销和恢复</a></li>
<li><a href="#sou-suo-ti-huan">搜索替换</a></li>
<li><a href="#shan-chu-jian-qie">删除剪切</a></li>
<li><a href="#fu-zhi-zhan-tie">复制粘贴</a></li>
</ul>
</li>
<li><a href="#shell-shu-chu-dai-yan-se-de-zi-ti">shell输出带颜色的字体</a><ul>
<li><a href="#te-xiao-ge-shi">特效格式</a></li>
<li><a href="#yan-se-bian-ma">颜色编码</a></li>
<li><a href="#xiao-guo-shi-li">效果示例</a></li>
<li><a href="#xiu-gai-ming-ling-ti-shi-fu-yan-se">修改命令提示符颜色</a></li>
</ul>
</li>
</ul>
</div>
<p>一些比较基本的，也没有特别归类的小知识点，就往这篇里记录吧。  </p>
<h3 id="linux-wen-jian-xi-tong"><a class="toclink" href="#linux-wen-jian-xi-tong">Linux 文件系统</a></h3>
<p><strong>常见Linux目录名称：</strong></p>
<table>
<thead>
<tr>
<th align="left">目录</th>
<th align="left">用途</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">/</td>
<td align="left">虚拟目录的根目录。通常不会在这里存储文件</td>
</tr>
<tr>
<td align="left">/bin</td>
<td align="left">二进制目录，存放许多用户级的GUN工具</td>
</tr>
<tr>
<td align="left">/boot</td>
<td align="left">启动目录，存放启动文件</td>
</tr>
<tr>
<td align="left">/dev</td>
<td align="left">设备目录，Linux在这里创建设备节点</td>
</tr>
<tr>
<td align="left">/etc</td>
<td align="left">系统配置文件目录</td>
</tr>
<tr>
<td align="left">/home</td>
<td align="left">主目录，Linux在这里创建用户目录</td>
</tr>
<tr>
<td align="left">/lib</td>
<td align="left">库目录，存放系统和应用程序的库文件</td>
</tr>
<tr>
<td align="left">/media</td>
<td align="left">媒体目录，可移动媒体设备的常用挂载点</td>
</tr>
<tr>
<td align="left">/mnt</td>
<td align="left">挂载目录，另一个可移动媒体设备的常用挂载点</td>
</tr>
<tr>
<td align="left">/opt</td>
<td align="left">可选目录，常用于存放第三方软件包和数据文件</td>
</tr>
<tr>
<td align="left">/proc</td>
<td align="left">进程目录，存放现有硬件及当前进程的相关信息</td>
</tr>
<tr>
<td align="left">/root</td>
<td align="left">root用户的主目录</td>
</tr>
<tr>
<td align="left">/sbin</td>
<td align="left">系统二进制目录，存放许多GUN管理员级工具</td>
</tr>
<tr>
<td align="left">/run</td>
<td align="left">运行目录，存放系统运作时的运行时数据</td>
</tr>
<tr>
<td align="left">/srv</td>
<td align="left">服务目录，存放本地服务的相关文件</td>
</tr>
<tr>
<td align="left">/sys</td>
<td align="left">系统目录，存放系统硬件信息的相关文件</td>
</tr>
<tr>
<td align="left">/tmp</td>
<td align="left">临时目录，可以在该目录中创建和删除临时工作文件</td>
</tr>
<tr>
<td align="left">/usr</td>
<td align="left">用户二进制目录，大量用户级的GUN工具和数据文件都存储在这里</td>
</tr>
<tr>
<td align="left">/var</td>
<td align="left">可变目录，用以存放经常变化的文件，比如日志文件</td>
</tr>
</tbody>
</table>
<p>常见的目录名均基于文件系统层级标准（filesystem hierarchy stanard, FHS）。FHS偶尔会进行更新，可能有些Linux发行版仍在使用旧的FHS标准。最新的FHS标准可以访问其官方主页：<br>
<a href="http://www.pathname.com/fhs">http://www.pathname.com/fhs</a>  </p>
<h3 id="jie-shu-jin-cheng"><a class="toclink" href="#jie-shu-jin-cheng">结束进程</a></h3>
<p>在Linux中，进程之间通过<strong>信号</strong>来通信。大多数编写完善的程序都能接收和处理标准Unix进程信号。  </p>
<h4 id="linux-jin-cheng-xin-hao"><a class="toclink" href="#linux-jin-cheng-xin-hao">Linux进程信号</a></h4>
<p>下面的表格是一些常用的信号：</p>
<table>
<thead>
<tr>
<th align="left">信号</th>
<th align="left">名称</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">1</td>
<td align="left">HUP</td>
<td align="left">挂起</td>
</tr>
<tr>
<td align="left">2</td>
<td align="left">INT</td>
<td align="left">中断</td>
</tr>
<tr>
<td align="left">3</td>
<td align="left">QUIT</td>
<td align="left">结束运行</td>
</tr>
<tr>
<td align="left">9</td>
<td align="left">KILL</td>
<td align="left">无条件终止</td>
</tr>
<tr>
<td align="left">11</td>
<td align="left">SEGV</td>
<td align="left">段错误</td>
</tr>
<tr>
<td align="left">15</td>
<td align="left">TERM</td>
<td align="left">尽可能终止</td>
</tr>
<tr>
<td align="left">17</td>
<td align="left">STOP</td>
<td align="left">无条件停止运行，但不终止</td>
</tr>
<tr>
<td align="left">18</td>
<td align="left">TSTP</td>
<td align="left">停止或暂停，但继续在后台运行</td>
</tr>
<tr>
<td align="left">19</td>
<td align="left">CONT</td>
<td align="left">在STOP或TSTP之后恢复执行</td>
</tr>
</tbody>
</table>
<p>HUP信号有两个用途（两种情况）：<br>
通知程序与它的控制终端（Session）不再关联。登录系统时会分配给用户一个终端，在shell下执行的命令，就是和当前的终端关联的。用户终端连接(正常或非正常)结束时，就会用这个信号发出通知。而这个信号的默认操作就是终止进程。<br>
而对于与终端脱离关系的守护进程，这个信号用于通知它重新读取配置文件。<em>程序可以捕获这个信号，然后重载配置，从而避免重启来是最新配置生效。前提是程序有做这个优化。</em>  </p>
<p>上面的信号不全，可以用命令<code>kill -l</code>查看全部的信号。<br>
还可以用man命令查看信号的说明：<code>man 7 signal</code>。  </p>
<p>下面是一份完整的：</p>
<div class="highlight"><pre><span></span><code>$ <span class="nb">kill</span> -l 
 <span class="m">1</span><span class="o">)</span> SIGHUP       <span class="m">2</span><span class="o">)</span> SIGINT       <span class="m">3</span><span class="o">)</span> SIGQUIT      <span class="m">4</span><span class="o">)</span> SIGILL       <span class="m">5</span><span class="o">)</span> SIGTRAP
 <span class="m">6</span><span class="o">)</span> SIGABRT      <span class="m">7</span><span class="o">)</span> SIGBUS       <span class="m">8</span><span class="o">)</span> SIGFPE       <span class="m">9</span><span class="o">)</span> SIGKILL     <span class="m">10</span><span class="o">)</span> SIGUSR1
<span class="m">11</span><span class="o">)</span> SIGSEGV     <span class="m">12</span><span class="o">)</span> SIGUSR2     <span class="m">13</span><span class="o">)</span> SIGPIPE     <span class="m">14</span><span class="o">)</span> SIGALRM     <span class="m">15</span><span class="o">)</span> SIGTERM
<span class="m">16</span><span class="o">)</span> SIGSTKFLT   <span class="m">17</span><span class="o">)</span> SIGCHLD     <span class="m">18</span><span class="o">)</span> SIGCONT     <span class="m">19</span><span class="o">)</span> SIGSTOP     <span class="m">20</span><span class="o">)</span> SIGTSTP
<span class="m">21</span><span class="o">)</span> SIGTTIN     <span class="m">22</span><span class="o">)</span> SIGTTOU     <span class="m">23</span><span class="o">)</span> SIGURG      <span class="m">24</span><span class="o">)</span> SIGXCPU     <span class="m">25</span><span class="o">)</span> SIGXFSZ
<span class="m">26</span><span class="o">)</span> SIGVTALRM   <span class="m">27</span><span class="o">)</span> SIGPROF     <span class="m">28</span><span class="o">)</span> SIGWINCH    <span class="m">29</span><span class="o">)</span> SIGIO       <span class="m">30</span><span class="o">)</span> SIGPWR
<span class="m">31</span><span class="o">)</span> SIGSYS      <span class="m">34</span><span class="o">)</span> SIGRTMIN    <span class="m">35</span><span class="o">)</span> SIGRTMIN+1  <span class="m">36</span><span class="o">)</span> SIGRTMIN+2  <span class="m">37</span><span class="o">)</span> SIGRTMIN+3
<span class="m">38</span><span class="o">)</span> SIGRTMIN+4  <span class="m">39</span><span class="o">)</span> SIGRTMIN+5  <span class="m">40</span><span class="o">)</span> SIGRTMIN+6  <span class="m">41</span><span class="o">)</span> SIGRTMIN+7  <span class="m">42</span><span class="o">)</span> SIGRTMIN+8
<span class="m">43</span><span class="o">)</span> SIGRTMIN+9  <span class="m">44</span><span class="o">)</span> SIGRTMIN+10 <span class="m">45</span><span class="o">)</span> SIGRTMIN+11 <span class="m">46</span><span class="o">)</span> SIGRTMIN+12 <span class="m">47</span><span class="o">)</span> SIGRTMIN+13
<span class="m">48</span><span class="o">)</span> SIGRTMIN+14 <span class="m">49</span><span class="o">)</span> SIGRTMIN+15 <span class="m">50</span><span class="o">)</span> SIGRTMAX-14 <span class="m">51</span><span class="o">)</span> SIGRTMAX-13 <span class="m">52</span><span class="o">)</span> SIGRTMAX-12
<span class="m">53</span><span class="o">)</span> SIGRTMAX-11 <span class="m">54</span><span class="o">)</span> SIGRTMAX-10 <span class="m">55</span><span class="o">)</span> SIGRTMAX-9  <span class="m">56</span><span class="o">)</span> SIGRTMAX-8  <span class="m">57</span><span class="o">)</span> SIGRTMAX-7
<span class="m">58</span><span class="o">)</span> SIGRTMAX-6  <span class="m">59</span><span class="o">)</span> SIGRTMAX-5  <span class="m">60</span><span class="o">)</span> SIGRTMAX-4  <span class="m">61</span><span class="o">)</span> SIGRTMAX-3  <span class="m">62</span><span class="o">)</span> SIGRTMAX-2
<span class="m">63</span><span class="o">)</span> SIGRTMAX-1  <span class="m">64</span><span class="o">)</span> SIGRTMAX
$ 
</code></pre></div>

<p>以信号1为例，这里是SIGHUP，上面的表格里是HUP，这两个名字是一样的。  </p>
<h4 id="kill-ming-ling"><a class="toclink" href="#kill-ming-ling">kill命令</a></h4>
<p>kill命令可通过进程ID（PID）给进程发信号。默认发送(15)TERM信号。因为只能使用PID来指定，而不能用命令名，所以有时候不是很好用。<br>
要发送信号，你还必须是该进程的属主或者是root。<br>
kill命令不会有任何输出，要检查kill命令是由有效，可以再运行ps或top命令:</p>
<div class="highlight"><pre><span></span><code>$ ps -fp PID 
</code></pre></div>

<h4 id="killall-ming-ling"><a class="toclink" href="#killall-ming-ling">killall命令</a></h4>
<p>killall命令支持通过进程名而不是PID来结束进程。killall命令也支持通配符。<br>
最小安装是没有这个命令的，yum安装一下：</p>
<div class="highlight"><pre><span></span><code>yum install psmisc
</code></pre></div>

<h3 id="vi-bian-ji-qi"><a class="toclink" href="#vi-bian-ji-qi">vi编辑器</a></h3>
<p>vi是Linux自带的文本编辑器。功能强大，但是只有命令，没有菜单。</p>
<h4 id="xiu-gai-wen-ben"><a class="toclink" href="#xiu-gai-wen-ben">修改文本</a></h4>
<table>
<thead>
<tr>
<th align="left">按键</th>
<th align="left">效果</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">i</td>
<td align="left">在光标前插入</td>
</tr>
<tr>
<td align="left">I</td>
<td align="left">在当前行行首插入</td>
</tr>
<tr>
<td align="left">a</td>
<td align="left">在光标后插入</td>
</tr>
<tr>
<td align="left">A</td>
<td align="left">在当前行末尾插入</td>
</tr>
<tr>
<td align="left">o</td>
<td align="left">在下一行插入新行</td>
</tr>
<tr>
<td align="left">O</td>
<td align="left">在上一行插入新行</td>
</tr>
<tr>
<td align="left">r&lt;字符&gt;</td>
<td align="left">替换光标所在位置的一个字符</td>
</tr>
<tr>
<td align="left">R</td>
<td align="left">从光标位置开始替换，按Esc结束</td>
</tr>
</tbody>
</table>
<h4 id="ding-wei-ming-ling"><a class="toclink" href="#ding-wei-ming-ling">定位命令</a></h4>
<table>
<thead>
<tr>
<th align="left">命令</th>
<th align="left">作用</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">hjkl</td>
<td align="left">对应左下上右移动</td>
</tr>
<tr>
<td align="left">w, b</td>
<td align="left">向前向后移动到词首</td>
</tr>
<tr>
<td align="left">e, ge</td>
<td align="left">向前向后移动到词尾</td>
</tr>
<tr>
<td align="left">0, $</td>
<td align="left">移动到行首，行尾</td>
</tr>
<tr>
<td align="left">:set nu</td>
<td align="left">显示行号</td>
</tr>
<tr>
<td align="left">:set noun</td>
<td align="left">取消显示行号</td>
</tr>
<tr>
<td align="left">:set ic</td>
<td align="left">忽略大小写</td>
</tr>
<tr>
<td align="left">:set noic</td>
<td align="left">取消忽略大小写</td>
</tr>
<tr>
<td align="left">:set hls</td>
<td align="left">匹配项高亮显示</td>
</tr>
<tr>
<td align="left">:set is</td>
<td align="left">显示部分匹配</td>
</tr>
<tr>
<td align="left">gg</td>
<td align="left">跳到首行</td>
</tr>
<tr>
<td align="left">G</td>
<td align="left">跳到尾行</td>
</tr>
<tr>
<td align="left">:n, nG, ngg</td>
<td align="left">n为数值，跳到第n行</td>
</tr>
<tr>
<td align="left">n&lt;回车&gt;</td>
<td align="left">n为数值，向下移动n行</td>
</tr>
</tbody>
</table>
<p>移动命令默认移动一次，前面可以加数字移动n次，比如3w移动3个词。  </p>
<h4 id="che-xiao-he-hui-fu"><a class="toclink" href="#che-xiao-he-hui-fu">撤销和恢复</a></h4>
<table>
<thead>
<tr>
<th align="left">按键</th>
<th align="left">效果</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">u</td>
<td align="left">undo，撤销上一步</td>
</tr>
<tr>
<td align="left">Ctrl+r</td>
<td align="left">redo，恢复上一步的撤销</td>
</tr>
</tbody>
</table>
<p>可以记录下多个步骤，回退到n步之前的状态。  </p>
<h4 id="sou-suo-ti-huan"><a class="toclink" href="#sou-suo-ti-huan">搜索替换</a></h4>
<table>
<thead>
<tr>
<th align="left">命令</th>
<th align="left">作用</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">/word</td>
<td align="left">向下搜索</td>
</tr>
<tr>
<td align="left">?word</td>
<td align="left">向上搜索</td>
</tr>
<tr>
<td align="left">%</td>
<td align="left">查找配对的括号 {, [, (</td>
</tr>
<tr>
<td align="left">n</td>
<td align="left">搜索下一个。如果是?，方向是反的</td>
</tr>
<tr>
<td align="left">N</td>
<td align="left">搜索上一个。如果是?，方向是反的</td>
</tr>
<tr>
<td align="left">:s/old/new</td>
<td align="left">替换该行第一个匹配</td>
</tr>
<tr>
<td align="left">:s/old/new/g</td>
<td align="left">替换该行所有的匹配</td>
</tr>
<tr>
<td align="left">:%s/old/new</td>
<td align="left">前面加上%，匹配所有的行，但每行只匹配第一个</td>
</tr>
<tr>
<td align="left">:%s/old/new/g</td>
<td align="left">后面加上g，每行可匹配多次</td>
</tr>
</tbody>
</table>
<p>替换略复杂，个人习惯在文件外使用sed命令，因为基本不会考虑用vi对文件做复杂的操作，而且sed命令可以写在脚本里用。  </p>
<h4 id="shan-chu-jian-qie"><a class="toclink" href="#shan-chu-jian-qie">删除剪切</a></h4>
<table>
<thead>
<tr>
<th align="left">按键</th>
<th align="left">效果</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">x</td>
<td align="left">删除光标位置的一个字符</td>
</tr>
<tr>
<td align="left">nx</td>
<td align="left">n为数字，删除后面n个字符</td>
</tr>
<tr>
<td align="left">X</td>
<td align="left">删除前一个字符，退格键</td>
</tr>
<tr>
<td align="left">dw, db, de, dge</td>
<td align="left">从当前字符位置删除至词尾词尾，参考光标的移动</td>
</tr>
<tr>
<td align="left">d0, d$</td>
<td align="left">删除至行首或行尾</td>
</tr>
<tr>
<td align="left">dd</td>
<td align="left">删除当前行</td>
</tr>
<tr>
<td align="left">ndd</td>
<td align="left">n为数字，删除n行</td>
</tr>
<tr>
<td align="left">dgg</td>
<td align="left">删除当前行在首行的所有行</td>
</tr>
<tr>
<td align="left">dG</td>
<td align="left">删除当前行为尾行的所有行</td>
</tr>
<tr>
<td align="left">D</td>
<td align="left">从光标当前位置删到行尾</td>
</tr>
<tr>
<td align="left">:n1,n2d</td>
<td align="left">n1,n2为数字，删除指定范围的行</td>
</tr>
</tbody>
</table>
<p>这里同时也是剪切，可以配合下面的粘贴使用。<br>
参考光标的移动，前面加上d，就是删除到那个位置。  </p>
<h4 id="fu-zhi-zhan-tie"><a class="toclink" href="#fu-zhi-zhan-tie">复制粘贴</a></h4>
<table>
<thead>
<tr>
<th align="left">按键</th>
<th align="left">效果</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">d, y</td>
<td align="left">剪切，复制，后面更光标移动指令</td>
</tr>
<tr>
<td align="left">yy</td>
<td align="left">复制当前行，内容不会有任何改变</td>
</tr>
<tr>
<td align="left">nyy</td>
<td align="left">n为数值，复制n行</td>
</tr>
<tr>
<td align="left">p</td>
<td align="left">粘贴到下一行，光标移动到下一行</td>
</tr>
<tr>
<td align="left">P</td>
<td align="left">粘贴到上一行，光标移动到上一行</td>
</tr>
<tr>
<td align="left">ygg,yG</td>
<td align="left">复制直到首行/末行</td>
</tr>
</tbody>
</table>
<h3 id="shell-shu-chu-dai-yan-se-de-zi-ti"><a class="toclink" href="#shell-shu-chu-dai-yan-se-de-zi-ti">shell输出带颜色的字体</a></h3>
<p>在命令行中输出彩色效果的字体。Windows的cmd不支持，Git Bash默认就能使用。<br>
而SecureCRT，默认效果是不开启的，需要修改会话的设置：<br>
<img alt="英文设置" src="/images/linux/01_SecureCRT_setting_en.png"><br>
<img alt="中文设置" src="/images/linux/01_SecureCRT_setting_cn.png">  </p>
<p>默认的设置，效果也能有，但是不支持彩色。<br>
勾选ANSI彩色选项，就能出彩色的效果，其他的几个勾可以试下效果，是别的颜色方面的设置。  </p>
<h4 id="te-xiao-ge-shi"><a class="toclink" href="#te-xiao-ge-shi">特效格式</a></h4>
<p>颜色另外说，这里是闪烁、高亮这类特效效果。  </p>
<h5 id="shu-chu-te-xiao-ge-shi-kong-zhi"><a class="toclink" href="#shu-chu-te-xiao-ge-shi-kong-zhi">输出特效格式控制</a></h5>
<table>
<thead>
<tr>
<th align="left">符号</th>
<th align="left">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">\033[0m</td>
<td align="left">关闭所有属性</td>
</tr>
<tr>
<td align="left">\033[1m</td>
<td align="left">设置高亮度</td>
</tr>
<tr>
<td align="left">\03[4m</td>
<td align="left">下划线</td>
</tr>
<tr>
<td align="left">\033[5m</td>
<td align="left">闪烁</td>
</tr>
<tr>
<td align="left">\033[7m</td>
<td align="left">反显</td>
</tr>
<tr>
<td align="left">\033[8m</td>
<td align="left">消隐</td>
</tr>
<tr>
<td align="left">\033[30m ~ \033[37m</td>
<td align="left">设置前景色</td>
</tr>
<tr>
<td align="left">\033[40m ~ \033[47m</td>
<td align="left">设置背景色</td>
</tr>
</tbody>
</table>
<p>&nbsp;  </p>
<h5 id="guang-biao-wei-zhi-deng-de-ge-shi-kong-zhi"><a class="toclink" href="#guang-biao-wei-zhi-deng-de-ge-shi-kong-zhi">光标位置等的格式控制</a></h5>
<table>
<thead>
<tr>
<th align="left">符号</th>
<th align="left">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">\033[nA</td>
<td align="left">光标上移n行</td>
</tr>
<tr>
<td align="left">\03[nB</td>
<td align="left">光标下移n行</td>
</tr>
<tr>
<td align="left">\033[nC</td>
<td align="left">光标右移n行</td>
</tr>
<tr>
<td align="left">\033[nD</td>
<td align="left">光标左移n行</td>
</tr>
<tr>
<td align="left">\033[y;xH</td>
<td align="left">设置光标位置</td>
</tr>
<tr>
<td align="left">\033[2J</td>
<td align="left">清屏</td>
</tr>
<tr>
<td align="left">\033[K</td>
<td align="left">清除从光标到行尾的内容</td>
</tr>
<tr>
<td align="left">\033[s</td>
<td align="left">保存光标位置</td>
</tr>
<tr>
<td align="left">\033[u</td>
<td align="left">恢复光标位置</td>
</tr>
<tr>
<td align="left">\033[?25l</td>
<td align="left">隐藏光标</td>
</tr>
<tr>
<td align="left">\33[?25h</td>
<td align="left">显示光标</td>
</tr>
</tbody>
</table>
<h4 id="yan-se-bian-ma"><a class="toclink" href="#yan-se-bian-ma">颜色编码</a></h4>
<p>编码：</p>
<ul>
<li>0: 重新设置属性到缺省设置</li>
<li>1: 设置粗体</li>
<li>2: 设置一半亮度(模拟彩色显示器的颜色)</li>
<li>4: 设置下划线(模拟彩色显示器的颜色)</li>
<li>5: 设置闪烁</li>
<li>7: 设置反向图象</li>
<li>22: 设置一般密度</li>
<li>24: 关闭下划线</li>
<li>25: 关闭闪烁</li>
<li>27: 关闭反向图象</li>
<li>30: 设置黑色前景</li>
<li>31: 设置红色前景</li>
<li>32: 设置绿色前景</li>
<li>33: 设置棕色前景</li>
<li>34: 设置蓝色前景</li>
<li>35: 设置紫色前景</li>
<li>36: 设置青色前景</li>
<li>37: 设置白色前景</li>
<li>38: 在缺省的前景颜色上设置下划线</li>
<li>39: 在缺省的前景颜色上关闭下划线</li>
<li>40: 设置黑色背景</li>
<li>41: 设置红色背景</li>
<li>42: 设置绿色背景</li>
<li>43: 设置棕色背景</li>
<li>44: 设置蓝色背景</li>
<li>45: 设置紫色背景</li>
<li>46: 设置青色背景</li>
<li>47: 设置白色背景</li>
<li>49: 设置缺省黑色背景</li>
</ul>
<p>以上特效可以叠加，需要使用分号（;）隔开，例如：<br>
闪烁+下划线+白底色+黑字为，\033[5;4;47;30m酷炫的效果字样\033[0m</p>
<h4 id="xiao-guo-shi-li"><a class="toclink" href="#xiao-guo-shi-li">效果示例</a></h4>
<p>下面是一段小例子：</p>
<div class="highlight"><pre><span></span><code><span class="ch">#!/bin/bash</span>
<span class="c1">#</span>
<span class="c1">#下面是字体输出颜色及终端格式控制</span>
<span class="c1">#字体色范围：30-37</span>
<span class="nb">echo</span> -e <span class="s2">&quot;\033[30m 黑色字 \033[0m&quot;</span>
<span class="nb">echo</span> -e <span class="s2">&quot;\033[31m 红色字 \033[0m&quot;</span>
<span class="nb">echo</span> -e <span class="s2">&quot;\033[32m 绿色字 \033[0m&quot;</span>
<span class="nb">echo</span> -e <span class="s2">&quot;\033[33m 黄色字 \033[0m&quot;</span>
<span class="nb">echo</span> -e <span class="s2">&quot;\033[34m 蓝色字 \033[0m&quot;</span>
<span class="nb">echo</span> -e <span class="s2">&quot;\033[35m 紫色字 \033[0m&quot;</span>
<span class="nb">echo</span> -e <span class="s2">&quot;\033[36m 天蓝字 \033[0m&quot;</span>
<span class="nb">echo</span> -e <span class="s2">&quot;\033[37m 白色字 \033[0m&quot;</span>
<span class="c1">#字背景颜色范围：40-47</span>
<span class="nb">echo</span> -e <span class="s2">&quot;\033[40;37m 黑底白字 \033[0m&quot;</span>
<span class="nb">echo</span> -e <span class="s2">&quot;\033[41;30m 红底黑字 \033[0m&quot;</span>
<span class="nb">echo</span> -e <span class="s2">&quot;\033[42;34m 绿底蓝字 \033[0m&quot;</span>
<span class="nb">echo</span> -e <span class="s2">&quot;\033[43;34m 黄底蓝字 \033[0m&quot;</span>
<span class="nb">echo</span> -e <span class="s2">&quot;\033[44;30m 蓝底黑字 \033[0m&quot;</span>
<span class="nb">echo</span> -e <span class="s2">&quot;\033[45;30m 紫底黑字 \033[0m&quot;</span>
<span class="nb">echo</span> -e <span class="s2">&quot;\033[46;30m 天蓝底黑字 \033[0m&quot;</span>
<span class="nb">echo</span> -e <span class="s2">&quot;\033[47;34m 白底蓝字 \033[0m&quot;</span>

<span class="c1">#控制选项说明</span>
<span class="c1">#\033[0m 关闭所有属性</span>
<span class="c1">#\033[1m 设置高亮度</span>
<span class="c1">#\033[4m 下划线</span>
<span class="nb">echo</span> -e <span class="s2">&quot;\033[4;31m 下划线红字 \033[0m&quot;</span>
<span class="c1">#闪烁</span>
<span class="nb">echo</span> -e <span class="s2">&quot;\033[5;34m 红字在闪烁 \033[0m&quot;</span>
<span class="c1">#反影</span>
<span class="nb">echo</span> -e <span class="s2">&quot;\033[8m 消隐 \033[0m &quot;</span>

<span class="c1">#\033[30m-\033[37m 设置前景色</span>
<span class="c1">#\033[40m-\033[47m 设置背景色</span>
<span class="c1">#\033[nA光标上移n行</span>
<span class="c1">#\033[nB光标下移n行</span>
<span class="nb">echo</span> -e <span class="s2">&quot;\033[4A 光标上移4行 \033[0m&quot;</span>
<span class="c1">#\033[nC光标右移n行</span>
<span class="c1">#\033[nD光标左移n行</span>
<span class="c1">#\033[y;xH设置光标位置</span>
<span class="c1">#\033[2J清屏</span>
<span class="c1">#\033[K清除从光标到行尾的内容</span>
<span class="nb">echo</span> -e <span class="s2">&quot;\033[K 清除光标到行尾的内容 \033[0m&quot;</span>
<span class="c1">#\033[s 保存光标位置</span>
<span class="c1">#\033[u 恢复光标位置</span>
<span class="c1">#\033[?25| 隐藏光标</span>
<span class="c1">#\033[?25h 显示光标</span>
<span class="nb">echo</span> -e <span class="s2">&quot;\033[?25l 隐藏光标 \033[0m&quot;</span>
<span class="nb">echo</span> -e <span class="s2">&quot;\033[?25h 显示光标 \033[0m&quot;</span>
</code></pre></div>

<h4 id="xiu-gai-ming-ling-ti-shi-fu-yan-se"><a class="toclink" href="#xiu-gai-ming-ling-ti-shi-fu-yan-se">修改命令提示符颜色</a></h4>
<p>命令提示符由环境变量PS1决定。<br>
系统层面在配置文件<code>/etc/bashrc</code>中指定，用户层面在配置文件<code>~/.bashrc</code>中修改。<br>
<em>因为要保证切换用户是也能生效，就是non-login方式也能生效，所以要放在bashrc中。参考Ubuntu和Centos的默认配置，也是这么做的。</em>  </p>
<p>在Centos7的<code>/etc/bashrc</code>中，有如下代码片段可以参考：</p>
<div class="highlight"><pre><span></span><code><span class="o">[</span> <span class="s2">&quot;</span><span class="nv">$PS1</span><span class="s2">&quot;</span> <span class="o">=</span> <span class="s2">&quot;\\s-\\v\\\$ &quot;</span> <span class="o">]</span> <span class="o">&amp;&amp;</span> <span class="nv">PS1</span><span class="o">=</span><span class="s2">&quot;[\u@\h \W]\\</span>$<span class="s2"> &quot;</span>
<span class="c1"># You might want to have e.g. tty in prompt (e.g. more virtual machines)</span>
<span class="c1"># and console windows</span>
<span class="c1"># If you want to do so, just add e.g.</span>
<span class="c1"># if [ &quot;$PS1&quot; ]; then</span>
<span class="c1">#   PS1=&quot;[\u@\h:\l \W]\\$ &quot;</span>
<span class="c1"># fi</span>
<span class="c1"># to your custom modification shell script in /etc/profile.d/ directory</span>
</code></pre></div>

<p>可以把如下的代码放在<code>~/.bashrc</code>文件中，设置提示符的颜色：</p>
<div class="highlight"><pre><span></span><code><span class="k">if</span> <span class="o">[</span> <span class="s2">&quot;</span><span class="nv">$PS1</span><span class="s2">&quot;</span> <span class="o">]</span><span class="p">;</span> <span class="k">then</span>
    <span class="nv">PS1</span><span class="o">=</span><span class="s2">&quot;\[\033[33m\]</span><span class="nv">$PS1</span><span class="s2">\[\033[0m\]&quot;</span>
<span class="k">fi</span>
</code></pre></div>

<p>这里加了转义的中括号把颜色格式的代码包起来了。<br>
<strong>注意：</strong>如果加了颜色的格式，在命令太长超过一行以后，显示会有问题，不会在下一行显示，而是回到当前行的开头覆盖之前的内容。在外层包上转义的中括号就好了。  </p>
      </div>
      <div class="back-to-top">
        <a href="/">HOME</a>
        <a href="#top">TOP</a>
      </div>
    </article>
  </div>
<!-- end article -->
<!-- 页面往下滚动一段之后才会显示TOC -->
<script>
  window.onscroll = function() {
    var tocbox = document.getElementsByClassName('toc')[0];
    var osTop = document.documentElement.scrollTop || document.body.scrollTop;
    var osWidth = document.documentElement.scrollWidth || document.body.scrollWidth;
    // console.log(osTop)
    if (osTop>300 && osWidth>865) {
      tocbox.style.display = "block"
    }
    if (osTop<300 || osWidth<865) {
      tocbox.style.display = "none"
    }
  }
</script>
                <footer>
                    <div class="icons">
                    </div>
                    <span id="busuanzi_container_page_pv" style="padding: 10px">本文阅读量<span id="busuanzi_value_page_pv"></span>次</span>
                    <span id="busuanzi_container_site_pv" style="padding: 10px">本站总访问量<span id="busuanzi_value_site_pv"></span>次</span>
                    <span id="busuanzi_container_site_uv" style="padding: 10px">本站总访客数<span id="busuanzi_value_site_uv"></span>人</span>
                    <p>© <script>document.write(moment().format('YYYY'));</script> 749B</p>
                </footer>
        </div>
</body>
</html>